function generateHeatData(points, x, y, w, h, degree, grain, opacity) {
    const heatData = [];
    const sampleSize = grain; // Sample size of each grid cell

    for (let i = x + Math.floor(sampleSize / 2); i < x + w; i += sampleSize) {
        for (let j = y + Math.floor(sampleSize / 2); j < y + h; j += sampleSize) {
            const interpolatedTemp = calculateInterpolatedTemp(i, j, points, degree);
            heatData.push({
                x: i,
                y: j,
                intensity: interpolatedTemp,
                opacity: (1 - opacity) * interpolatedTemp + opacity
            });
        }
    }

    return heatData;
}

function calculateInterpolatedTemp(cx, cy, points, degree) {
    var maxTemp = 0;
    points.forEach(point => {
        const { x, y, temp } = point;
        const distance = Math.sqrt(Math.pow(x - cx, 2) + Math.pow(y - cy, 2));
        const cTemp = Math.max(0, temp - distance * degree);
        if (cTemp > maxTemp) {
            maxTemp = cTemp;
        }
    });
    return maxTemp;
}